package model.node;

import java.util.Random;

import controller.Hypervisor;
import model.exceptions.DeathNode;
import model.messages.LocationClaim;
import model.messages.Message;
import model.messages.MessageInterface;
import model.options.Options;

public class RedNode extends Node{

	/**
	 * Constructor require position of node and its id
	 * @param posX
	 * @param posY
	 */
	public RedNode(Hypervisor h,float posX, float posY, int idOfNode){
		super(h,posX, posY,idOfNode);
	}
	
	/**
	 * If node, receives a location claim: with probability 1-p just ignores the message 
	 * (no other actions), else selects a random point in the unit-square area and call routingProcedure method.
	 * If receive a message (no location claim), it call routingProcedure, and if is the target of node call checkForClone
	 */
	protected void manageMessage(MessageInterface message) throws DeathNode{
		String str;
		if(message instanceof LocationClaim){
			if (Math.random() <= Options.getInstance().getProbOfNeighbor()){
				str = "" + hypervisor.getRand() + message.getIdCreator();
				Random rand = new Random(new Long(str));
				for(int i=0; i < Options.getInstance().getNumDestination(); i++){
					Message m = new Message((LocationClaim)message,rand.nextFloat(),rand.nextFloat());
					if(routingProcedure(m)){
						checkForClone(m);
					}
				}
				hypervisor.decreaseMessages(1, this, message.getIdCreator()); //decrease the claim message received
			}
		}
		else if(routingProcedure((Message)message)){
			checkForClone(message);
		}
	}
}
